<?php

/// This is a function to check a user's roll to see if he can access the API
function getRolePermissions($con, $db_name, $requestVars,$sessionEnabled, $type)
{
    $rid  = $requestVars['rid'];
    
    if(!$rid)
    {
        if($type == 'json')
        {
            return GenericResponseJSON(11, "You must pass a role id (rid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(11, "You must pass a role id (rid) !");
        }        
    }
    
    $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    }  

    $query = "SELECT * FROM roles  WHERE ID = $rid";
    $result = mysqli_query($con,$query);

    if(!$result)
    {
        die("mysql error: ".mysql_error()."[$query]");
    }
    while($row = mysqli_fetch_assoc($result))
    { 
        $name = $row['name'];
        $Projects = sanitizeArray(unserialize($row["projects"]));
        $tasks = sanitizeArray(unserialize($row["tasks"]));
        $milestones = sanitizeArray(unserialize($row["milestones"]));
        $messages = sanitizeArray(unserialize($row["messages"]));
        $files = sanitizeArray(unserialize($row["files"]));
        $timetracker = sanitizeArray(unserialize($row["timetracker"]));
        $chat = sanitizeArray(unserialize($row["chat"]));
        $admin = sanitizeArray(unserialize($row["admin"]));     


    }
    if($type == 'json')
    {
        return RoleResponseJSON(1, "Success",$name, $Projects, $tasks, $milestones, $messages, $files, $timetracker, $chat, $admin);
    }
    else if($type == 'xml')
    {
        return RoleResponseXML(1, "Success",$name, $Projects, $tasks, $milestones, $messages, $files, $timetracker, $chat, $admin);
    }        
    
    
         
    

}

function CheckRoll($con, $db_name, $userid, $branch, $action)
{
    $query = "SELECT r.* FROM roles r join roles_assigned ra on ra.role = r.id  WHERE ra.user = $userid";
    $result = mysqli_query($con,$query);
 
    if(!$result)
    {
        die("mysql error: ".mysql_error()."[$query]");
    }
    while($row = mysqli_fetch_assoc($result))
    {     
        $brach_result = sanitizeArray(unserialize($row[$branch]));       

    }   
    if (empty($brach_result[$action]))
    {
        $brach_result[$action] = 0;
    }

    return $brach_result[$action];
    
}

function NoPermission($username,$branch,$action, $type)
{
    if($type == 'json')
    {
        return GenericResponseJSON(5,"User $username is not allowed to perform action $action for $branch");
    }
    else if($type == 'xml')
    {
        return GenericResponseXML(5,"User $username is not allowed to perform action $action for $branch");
    }
    
}

function addRoles($con, $db_name, $requestVars,$sessionEnabled,$type)
{
    if($type == 'json')
    {
        return GenericResponseJSON(6, "This method is not available in this version."); 
    }
    else if($type == 'xml')
    {
        return GenericResponseXML(6, "This method is not available in this version."); 
    }  
 
}

function editRoles($con, $db_name, $requestVars,$sessionEnabled)
{
    if($type == 'json')
    {
        return GenericResponseJSON(6, "This method is not available in this version."); 
    }
    else if($type == 'xml')
    {
        return GenericResponseXML(6, "This method is not available in this version."); 
    }  
}

function delRoles($con, $db_name, $requestVars,$sessionEnabled)
{
    if($type == 'json')
    {
        return GenericResponseJSON(6, "This method is not available in this version."); 
    }
    else if($type == 'xml')
    {
        return GenericResponseXML(6, "This method is not available in this version."); 
    }  
}
?>
